<?php
error_reporting(E_ALL & ~E_NOTICE);
ob_start();
//============ Start Session และทำการเรียก Function ติดต่อฐานข้อมูล 
session_start();
include("../connect/connect.php");

//เมื่อเข้าหน้าบิลจะเปลี่ยนสถานะเป็นเช็คบิลแล้ว
mysql_query("UPDATE table_status SET table_status_group = '1' WHERE table_status_table = '1' ");

	//สร้างเลขบิล
	$NoQuery = mysql_query("SELECT max(bill_id) as bill_id from bill");
	$BillRow = mysql_fetch_array($NoQuery);
	
	$BillQuery = mysql_query("SELECT bill_no1,bill_no2 FROM bill WHERE bill_id = '".$BillRow['bill_id']."' ");
	$BillNum = mysql_fetch_array($BillQuery);
	
	$BillNumId = $BillRow['bill_id']+1;
	$year = date("Y")+543;
	$month = date("m");
	$time = time();
	$day = date("Ymd", $time);
/*
	$d = date("H", $time);

	if($d<='5'){
		$ddate = strtotime("-1 day" , $time);
		$day = date("Ymd",  $ddate);
	} else {
		$day = date("Ymd", $time);
	}
*/
	
	if($BillNum['bill_no1']==$day){
		$BillNumber = $BillNum['bill_no2']+1;
		$resultNum=sprintf("%04d", $BillNumber);
		$billNo = $day.$resultNum;
		mysql_query("INSERT INTO bill (bill_no1,bill_no2,bill_no) VALUES ('".$day."','".$BillNumber."','".$billNo."')");
		mysql_query("UPDATE table_status SET bill_id = '".$BillNumId."' WHERE table_status_table = '1' ");
	} else {
		$BillNumber = '1';
		$resultNum=sprintf("%04d", $BillNumber);
		$billNo = $day.$resultNum;
		mysql_query("INSERT INTO bill (bill_no1,bill_no2,bill_no) VALUES ('".$day."','".$BillNumber."','".$billNo."')");
		mysql_query("UPDATE table_status SET bill_id = '".$BillNumId."' WHERE table_status_table = '1' ");
	}
	
	/*
	$BillNo = $BillRow['bill_id']+1;
	$result=sprintf("%05d", $BillNo);
	$month = date("m");
	$year = date("Y")+543;
	$day = date("d");
	$bill = $year.$month.$day.$result;
	$bill_Num = $year.$month.$day;
	
	$ChBill = mysql_query("SELECT bill_no1 FROM bill WHERE bill_no2 = '".$BillRow['bill_no2']."' ");
	$ChBillRow = mysql_fetch_array($ChBill);
	
	echo $BillNo;
	
	if($bill_Num==$ChBillRow['bill_no1']){
		echo $BillNo;
	} else {
		echo '1';
	}
	*/
	//echo $ChBillRow['bill_no1'];
//	mysql_query("INSERT INTO bill (bill_id,bill_no) VALUES ('".$BillNo."','".$bill."')");
	
	
if($_SESSION['user_username']=="" && $_SESSION["user_name"]=="" && $_SESSION["user_surname"]==""){
	echo "<script>window.location = '../index.php'</script>";
}

function utf8_substr($str,$start_p,$len_p) 
{ 
   preg_match_all("/./u", $str, $ar); 

   if(func_num_args() >= 3) { 
       $end = func_get_arg(2); 
       return join("",array_slice($ar[0],$start_p,$len_p)); 
   } else { 
       return join("",array_slice($ar[0],$start_p)); 
   }
}

function substr_utf8( $str, $start_p , $len_p){
 return preg_replace( '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$start_p.'}'.
  '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len_p.'}).*#s',
  '$1' , $str );
};

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>-</title>
<link rel="stylesheet" href="stylebill.css" type="text/css" />
<link rel="shortcut icon" href="../image/Letter-T.ico" />
</head>
<?php
/*
	$NoQuery = mysql_query("SELECT bill_no FROM bill,table_status WHERE table_status.bill_id = bill.bill_id AND table_status_table = '1'");
	$BillRow = mysql_fetch_array($NoQuery);
	$bill = $BillRow['bill_no'];
*/
?>
<body onload="window.print();">
<table class="mainbill" border="0" cellpadding="2" cellspacing="1" bgcolor="#666666" align="left">
  <tr>
    <td width="100%" valign="top" bgcolor="#FFFFFF">
	<div align="left" class="headbill"><strong>เลขที่ <?php echo $billNo; ?></strong></div>
      <p><strong><center>ใบแจ้งรายการค่าใช้จ่าย</center></strong></p>

<div align="right" class="headbill"><strong>วันที่</strong> :  <?php $y=date("Y")+543; echo date("d/m/").$y; ?>&nbsp; <strong>เวลา</strong> : <?php echo date("H:i:s"); ?>
<br />
<strong>แคชเชียร์</strong> : <?php echo $_SESSION["user_name"]." ".$_SESSION["user_surname"]; ?>
<?php 
/*
    $Query11 = mysql_query("SELECT table_status_sv FROM table_status WHERE table_status_table = '1'");
    $result11 = mysql_fetch_array($Query11);
    if($result11['table_status_sv']=='1'){
    	echo "<br><b>ค่าบริการ :</b> 10%";
    }
*/
?>
<br />
  <br />
</div>
  <div class="headbill"><?php
  $cQuery = "SELECT member.member_name as member_name,member.member_surname as member_surname FROM member,member_session,membertype WHERE membertype.memtype_id = member.memtype_id AND member.member_code=member_session.member_session_code AND member_session.member_session_table='1'";
  $cDbquery = mysql_query($cQuery);
  $cResult = mysql_fetch_assoc($cDbquery);
  if($cResult){
  	echo "<b>ชื่อลูกค้า :</b> ".$cResult['member_name']." ".$cResult['member_surname'];
  }

  $Query1 = "SELECT member_non_session.member_non_session_name as member_non_session_name,membertype.memtype_name as memtype_name FROM member_non_session,member,membertype WHERE member_non_session.member_non_session_code = member.member_code AND member.memtype_id = membertype.memtype_id AND member_non_session.member_non_session_table ='1'";
  $dbquery1 = mysql_query($Query1);
  $result1 = mysql_fetch_assoc($dbquery1);

  if($result1){
  	echo "<b>ชื่อลูกค้า :</b> ".$result1['member_non_session_name']."&nbsp;(".$result1['memtype_name'].")";
  }
?></div></p>
    <br />
	<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0" class="listorder">
  <thead>
  <tr>
    <td width="1">#</td>
    <td>รายการ</td>
    <td width="15%">@</td>
    <td width="18%">รวม</td>
  </tr>
  </thead>
 <?php

//============ ทำการแสงรายการที่ได้เลือกว่ามีกี่รายการ

$strTotal=0;
$cost=0;

//============ แสดงจำนวนรายการสินค้า
	$Query = "SELECT sale_session.sale_session_id as sale_session_id,sale_session.sale_session_prod as sale_session_prod,sum(sale_session.sale_session_qty) as sale_session_qty,sale_session.sale_session_table as sale_session_table,product.prod_barcode as prod_barcode,product.prod_name as prod_name,product.prod_price as prod_price,product.prodtype_id as prodtype_id FROM product,sale_session WHERE product.prod_barcode=sale_session.sale_session_prod GROUP BY sale_session.sale_session_prod ORDER BY sale_session.sale_session_id ASC";
	$dbquery = mysql_query($Query);
	
	while($result= mysql_fetch_array($dbquery))
		{
?>
  <tr>
    <td align="right" style="padding-right:5px"><?php echo $result['sale_session_qty']; ?></td>
    <td align="left" style="padding-left:7px"><?php echo substr_utf8($result['prod_name'],0,20);?></td>
    <td align="right" style="padding-right:7px"><?php $tol = $result["prod_price"]*$result['sale_session_qty']; echo number_format($result["prod_price"], 0,'.',','); ?></td>
<!-- 
    <td align="right" style="padding-right:5px"><?php
/*
    //คิดค่าบริการ 10%
    if($result['prodtype_id']=="5"){
    	$dc = '0';
    } else if($result11['table_status_sv']=='1'){

    	$dc = round(0.1*$tol);

    	//echo $result11['memtype_discount'];
    } else if($result11['table_status_sv']=='0') {
    	$dc = '0';
    }
    echo number_format($dc, 0,'.',',');
*/
?></td>

	<td align="right" style="padding-right:5px">
<?php 
/*
$mQuery = mysql_query("SELECT membertype.memtype_discount as memtype_discount FROM member_session,member,membertype WHERE member_session.member_session_code = member.member_code AND member.memtype_id = membertype.memtype_id AND member_session.member_session_table='1' ");
$mResult = mysql_fetch_array($mQuery);
if($mResult){
	if($result['prodtype_id']=="3"){
		$dis = '0';
	} else if($result['prodtype_id']=="4"){
		$dis = '0';
	} else if($result['prodtype_id']=="5"){
		$dis = '0';
	} else {
		$dis = round((1-$mResult['memtype_discount'])*$tol);
	}
	echo number_format($dis, 0,'.',',');
} else {
	echo "0";
}
*/
?>
	</td>
 -->
    <td align="right" style="padding-right:7px">
<?php
	//$total2 = ($tol+$dc);
/*
	if($result11['table_status_sv']=='1'){
	$total2 = $discount+($dc);
	} else if($result11['table_status_sv']=='0') {
	$total2 = $result['prod_price']*$result['sale_session_qty'];
	}
*/
	echo number_format($tol, 0,'.',',');
?></td>
	  </tr>
 <?php 
		$strTotal=$strTotal+$tol;
		}
?>
<tfoot>
  <tr>
    <td colspan="3" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:7px"><?php echo number_format($strTotal, 0,'.',','); ?></td>
	</tr>
</tfoot>
</table>
<?php if($cResult){ ?>
<div align="right" class="headbill">
<br />
<br />
สมาชิก (..................................)
</div>
<?php } ?>
</td>
  </tr>
</table>

</body>
</html>
